#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back

using namespace std;

using ll = long long;
using db = double;
using vec = vector<int>;
using pii = pair<int,int>;

int read() {
	int x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-',ch = getchar();
	while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48),ch = getchar();
	return f ? -x : x;
}

const int N = 1e5 + 10;
const int M = 1e6 + 10;

struct node { ll x,y; int pos; } ;
vector <node> val[N]; 
int x[M],y[M],p[M],q[M],n,m,hd[N];
ll dp[M],ans = 4e18,A,B,C;
vec tim[N],res[N];

ll calc (ll x) { return A * x * x + B * x + C; }

db slope (node a,node b) { return 1.0 * (a.y - b.y) / (1.0 * (a.x - b.x)); }

// xia tu ke
void ins (vector < node > &a,int hd,int id) { // not modify hd
	node t = (node) { q[id] , dp[id] - B * q[id] + A * q[id] * q[id] , id };
	
	int sz;
	
	while((sz = a.size()) - hd > 1 && slope(a[sz - 1] , a[sz - 2]) >= slope(a[sz - 2],t))
		a.pop_back();
	a.pb(t);
}

void limit(vector < node > & a,int &hd,db sl) { // modift hd 
	int sz; while((sz = a.size()) - hd > 1 && slope(a[hd] , a[hd + 1]) <= sl) hd++;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("route.in","r",stdin); freopen("route.out","w",stdout);
#endif
	n = in,m = in,A = in,B = in,C = in; int mxtime = 0;
	for(int i = 1;i <= m;i++) x[i] = in,y[i] = in,p[i] = in,q[i] = in,tim[p[i]].pb(i),mxtime = max(mxtime,q[i]);
	ins(val[1],hd[1],0);
	for(int t = 0;t <= mxtime;t++) {
		reverse(res[t].begin(),res[t].end());
		while(res[t].size()) ins(val[y[res[t].back()]],hd[y[res[t].back()]],res[t].back()),res[t].pop_back();
		for(auto d : tim[t]) {
			int u = x[d],v = y[d];
			if(val[u].size() == hd[u]) continue;
			limit(val[u],hd[u],2.0 * A * p[d]);
			int lst = val[u][hd[u]].pos;
			dp[d] = dp[lst] + calc(p[d] - q[lst]);
			res[q[d]].pb(d);
			if(v == n) ans = min(ans,dp[d] + q[d]);
		}
	}
	printf("%lld\n",ans);
	return 0;
}
